package fr.insee.solr.service.state;

import java.util.HashMap;
import java.util.Map;

import fr.insee.solr.model.NullFieldStrategy;
import fr.insee.solr.model.annotation.SolrField;

public class StateFactory {

	private final static Map<Class<? extends IState>, IState> states = new HashMap<Class<? extends IState>, IState>();
	
	static{
		states.put(StateSimple.class, new StateSimple());
		states.put(StateNested.class, new StateNested());
		states.put(StateSimpleStrategy.class, new StateSimpleStrategy());
		states.put(StateNestedStrategy.class, new StateNestedStrategy());
		states.put(StateSimpleMultivalued.class, new StateSimpleMultivalued());
		states.put(StateNestedMultivalued.class, new StateNestedMultivalued());
		states.put(StateSimpleMultivaluedStrategy.class, new StateSimpleMultivaluedStrategy());
		states.put(StateNestedMultivaluedStrategy.class, new StateNestedMultivaluedStrategy());
	}
	
	
	public static IState getState(SolrField sf){
		IState state = new NullState();
		boolean nestedBean = sf.beanName() != null && !sf.beanName().isEmpty();
		boolean noStrategy = sf.fieldStrategy().equals(NullFieldStrategy.class);
		if(!nestedBean && !sf.multivalued() && noStrategy && !sf.nestedList()){
			state = states.get(StateSimple.class);
		}else if(nestedBean && !sf.multivalued() && noStrategy && !sf.nestedList()){
			state = states.get(StateNested.class);
		}else if(!nestedBean && !sf.multivalued() && !noStrategy && !sf.nestedList()){
			state = states.get(StateSimpleStrategy.class);
		}else if(nestedBean && !sf.multivalued() && !noStrategy && !sf.nestedList()){
			state = states.get(StateNestedStrategy.class);
		}else if(!nestedBean && sf.multivalued() && noStrategy && !sf.nestedList()){
			state = states.get(StateSimpleMultivalued.class);
		}else if(nestedBean && sf.multivalued() && noStrategy && !sf.nestedList()){
			state = states.get(StateNestedMultivalued.class);
		}else if(!nestedBean && sf.multivalued() && !noStrategy && !sf.nestedList()){
			state = states.get(StateSimpleMultivaluedStrategy.class);
		}else if(nestedBean && sf.multivalued() && !noStrategy && !sf.nestedList()){
			state = states.get(StateNestedMultivaluedStrategy.class);
		}

		return state;
	}
}
